VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ClsInscrito"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Public WithEvents orset As ClsRecordset
Attribute orset.VB_VarHelpID = -1

Public Event DataChanged()

Public codigo As String
Public ci As String
Public nombre As String

Private Sub Class_Initialize()
    Set orset = New ClsRecordset
    orset.setCampos dbCampos(ins)
    orset.setTablas Array(ins)
End Sub
Private Sub Class_Terminate()
    Set orset = Nothing
End Sub
Public Sub consulta()
    orset.consulta
End Sub
Public Sub consultaDatos(codig As String)
    orset.consulta Array(ins_id & "=" & codig)
End Sub

Public Sub consultaInscripciones(codig As String)
    orset.consultaGral , Array(ins, tin), _
                       Array(tin_ins_id & "=" & ins_id)
End Sub
Public Sub consultaXClienteServicio(cliCodig As String, serCodig As String)
    orset.consultaGral , Array(ins, tra, tin, cue), _
                       Array(tin_ins_id & "=" & ins_id, _
                             tin_tra_id & "=" & tra_id, _
                             tra_cue_id & "=" & cue_id, _
                             cue_ser_id & "=" & serCodig, _
                             tra_cli_id & "='" & cliCodig & "'"), _
                       Array(ins_nombre)
End Sub

Public Sub inscribir(codig As String, traCodig As String)
    orset.sInsertGral Array(tin_tra_id, tin_ins_id), _
                      Array(traCodig, codig), _
                      Array(tin), False, False
End Sub

Public Sub buscarGrid(Optional optCi As String = "", Optional optNombre As String = "")
    Dim campos As Variant
    Dim tablas As Variant
    Dim condiciones As Variant
    Dim orden As Variant
    
    Dim camposBuscar As String
    Dim condicionesBuscar As String
    camposBuscar = ""
    condiciones = ""
    'insentes en gral
    camposBuscar = camposBuscar + "CASE WHEN "
        If optCi <> "" Then
            camposBuscar = camposBuscar + ins_ci + " LIKE '" + optCi + "%' AND "
        End If
        If optNombre <> "" Then
            camposBuscar = camposBuscar + ins_nombre + " LIKE '" + optNombre + "%' AND "
        End If
        If optCi = "" And optNombre = "" Then
            camposBuscar = camposBuscar + ins_ci + " LIKE '" + optCi + "%' AND "
        End If
    camposBuscar = Strings.Left(camposBuscar, Strings.Len(camposBuscar) - 4) & " THEN 1 "
        If optCi <> "" Then
            camposBuscar = camposBuscar + "WHEN " & ins_ci & " LIKE '" + optCi + "%' THEN 2 "
            condicionesBuscar = condicionesBuscar + ins_ci + " LIKE '" + optCi + "%' OR "
        End If
        If optNombre <> "" Then
            camposBuscar = camposBuscar & "WHEN " & ins_nombre & " LIKE '" + optNombre + "%' THEN 3 "
            condicionesBuscar = condicionesBuscar & ins_nombre & " LIKE '" + optNombre + "%' OR "
        End If
        If optCi = "" And optNombre = "" Then
            camposBuscar = camposBuscar + "WHEN " & ins_ci & " LIKE '" + optCi + "%' THEN 2 "
            condicionesBuscar = condicionesBuscar + ins_ci + " LIKE '" + optCi + "%' OR "
        End If
    condicionesBuscar = Strings.Left(condicionesBuscar, Strings.Len(condicionesBuscar) - 3)
    camposBuscar = camposBuscar + "END AS orden"
    
    campos = concatenaVectores(dbCampos(ins), Array(camposBuscar))
    tablas = Array(ins)
    condiciones = Array(condicionesBuscar)
    orden = Array("orden", ins_nombre)
    
    orset.consultaGral campos, tablas, condiciones, orden
End Sub

Public Sub borrarInscripcion(inscritoCodig As String, traCodig As String)
    orset.sDeleteGral Array(tin), _
                      Array(tin_tra_id & "='" & tra_id & "'", _
                            tin_ins_id & "=" & ins_id)
    'busca si la persona esta inscrita en otros servicios
    consultaInscripciones inscritoCodig
    If orset.numRegistros = 0 Then
        'si no la elimina
        Borrar inscritoCodig
    End If
End Sub

'
Public Sub Nuevo(newCi As String, newNombre As String)
    orset.sInsert Array("", newCi, newNombre)
End Sub
Public Sub Modificar(codig As String, newCi As String, newNombre As String)
    orset.sUpdate Array(codig, newCi, newNombre)
End Sub
Public Sub Borrar(codig As String)
    orset.sDelete Array(ins_id & "=" & codig)
End Sub
Private Sub Reload_members()
    With orset
        codigo = .getDato(ins_id)
        ci = .getDato(ins_ci)
        nombre = .getDato(ins_nombre)
    End With
    RaiseEvent DataChanged
End Sub
Private Sub orset_DataChanged()
    Reload_members
End Sub

